package com.example.myapplication.login.presenter;

import android.os.Handler;

import com.example.myapplication.login.model.LoginModel;
import com.example.myapplication.login.model.LoginModelImpl;
import com.example.myapplication.login.model.OnLoginListener;
import com.example.myapplication.login.model.User;
import com.example.myapplication.login.view.LoginView;

import javax.inject.Inject;


/**
 * Created by mantianxing on 2017/12/11.
 */

public class LoginPresenter {
    private LoginView loginView;
    private LoginModel loginModel;
    private Handler mHandler;

    @Inject
    LoginPresenter(LoginView loginView){
        this.loginView = loginView;
        this.loginModel = new LoginModelImpl();
        mHandler = new Handler();
    }

    @Inject
    void setupPresenterForView(){
        loginView.setPresenter(this);
    }
    public void login(){
        loginView.showLoading();
        loginModel.login(loginView.getUserName(), loginView.getPassword(), new OnLoginListener() {
            @Override
            public void loginSuccess(final User user) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.showSuccess(user);
                        loginView.hideLoading();
                    }
                });
            }

            @Override
            public void loginFailed(final String s) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        loginView.showFailedMsg(s);
                        loginView.hideLoading();
                    }
                });
            }
        });
    }

    public void clear(){
        loginView.clearEditText();
    }
}
